from itsdangerous import TimedJSONWebSignatureSerializer
from toutiao.settings import Config

#使用itsdangerous生成token
# def get_user_token(user_id):
#
#     s = TimedJSONWebSignatureSerializer(secret_key=Config.SECRET_KEY,expires_in=3600)
#     data = {
#         'user_id':user_id
#     }
#
#     return s.dumps(data).decode()  #字节类型字符串解码成字符串
#     # json.dump( )是对json文件的读写操作
#     # json.dumps( ）是对json字典转字符串

#使用第二种方法生成token,Jwt
from datetime import datetime,timedelta     #timedelta为时间间隔
from auth.auth_jwt import generate_jwt

def get_user_token(user_id):
    now = datetime.now()

    token_playload = {'refresh':False,'user_id':user_id}
    refresh_playload = {'refresh':True,'user_id':user_id}

    token_expiry = now + timedelta(hours=Config.JWT_EXPIRY_HOURS)
    refresh_expiry = now + timedelta(days=Config.JWT_REFRESH_DAYS)

    #2小时有效期
    token = generate_jwt(token_playload,token_expiry)
    #14天有效期
    refresh_token = generate_jwt(refresh_playload,refresh_expiry)

    return token,refresh_token


